import { _decorator, Component, Node, v3, v2 } from 'cc';
import { BehaviorStatus } from '../../../../extensions/oreo-behavior-creator/runtime/main';
import { AStarInfo } from '../../manage/tools/AStarGridTool';
import { IPlayerController } from '../IPlayerController';
import { IRoleController } from '../IRoleController';
const { ccclass, property } = _decorator;

//士兵
@ccclass('ISoldierController')
export class ISoldierController extends IRoleController {

    //跟随的玩家
    follow:IPlayerController = null;

    //跟随的最大距离(超出则加速追上)
    followMaxLen:number = 20;

    start() {

        super.start();
        this.limitMoveLen = 10;
        this.setFollow(this.follow);

    }


    setFollow(follow){

        if(!follow) return;

        this.follow = follow;
        //动态修改移动历史数
        this.follow.maxHistoryMoveNumber < this.followMaxLen && (this.follow.maxHistoryMoveNumber = this.followMaxLen);

    }

    //游走
    migration(){
        if(this.follow){
            return BehaviorStatus.Success;
        }else{
            return BehaviorStatus.Running;
        }
    }

    //追上军官
    chaseup(){

        let isNeedChase = ((Object.assign(v3(),
                    this.astar.worldToXY(this.node.worldPosition)
                ).subtract(Object.assign(v3(),
                    this.astar.worldToXY(this.follow.node.worldPosition))
                ).length() > this.followMaxLen));

        if(isNeedChase){

            //开始追赶
            this.speed = this.follow.speed * 2;

        }else{

            //没有追赶
            this.speed = this.follow.speed;

        }
        
        //如果移动并且需要追赶
        if(!this.isMove && isNeedChase){
            
            //移动到军官历史位置 如果历史没有位置则不进行移动
            if(this.follow.historyMoveInfos.length >= this.limitMoveLen){
                //移动到历史点
                this.moveXYPlayer(this.follow.historyMoveInfos[(this.follow.historyMoveInfos.length-this.limitMoveLen)]);

                for(let astar of this.follow.historyMoveInfos.slice((this.follow.historyMoveInfos.length-this.limitMoveLen),this.follow.historyMoveInfos.length-1)){
                    this.moveQueue.push(astar);
                }

            }else{

                let {x,y} = this.astar.worldToXY(this.follow.node.worldPosition);

                let pos = null;

                //如果军官之前没有移动过 则走到他四周
                for (let i = -(this.limitMoveLen); i <= (this.limitMoveLen); i++) {
                    for (let j = -(this.limitMoveLen); j <= (this.limitMoveLen); j++) {
                        
                        if(this.astar.grids[x+i][x+j] && !(this.astar.grids[x+i][x+j].isPrevent)){

                            pos = this.astar.grids[x+i][x+j];
                            break;

                        }

                    }

                    if(pos) break;
                }

                if(pos){
                    this.moveV2Player(v2(x,y));
                }

            }

            return BehaviorStatus.Running;
            
        }else{

            return BehaviorStatus.Success;

        }

    }

    //跟随军官
    followup(){


        let lastMove = this.moveQueue[this.moveQueue.length-1];
        let lastHistoryMove = this.follow.historyMoveInfos[this.follow.historyMoveInfos.length-1];

        if(!lastHistoryMove) return BehaviorStatus.Running;

        // if(this)
        if(lastMove && (lastMove.x != lastHistoryMove.x || lastMove.y != lastHistoryMove.y)){
            this.moveQueue.push(lastHistoryMove);
        }

        return BehaviorStatus.Success;

    }

}

